package com.bc.bcsellerapihttp.controller;

import com.bc.bcapi.dao.T_Integral_recordMapper;
import com.bc.bcapi.service.T_Integral_recordService;
import com.bc.bcapi.service.T_OrderService;
import com.bc.bcapi.service.T_Order_kycService;
import com.bc.bccore.webconfig.WebConfig;
import com.bc.bcmodel.SellerUserInfo;
import com.bc.bcmodel.domain.T_Order_kycModel;
import com.bc.bcmodel.domain.T_User_seller_infoModel;
import com.bc.bcmodel.enums.IntegralRecordEnum;
import com.bc.bcmodel.req.OrderExportReq;
import com.bc.bcmodel.req.OrderKycReq;
import com.bc.bcsellerapihttp.common.UserContext;
import com.bc.commoncore.util.StringUtils;
import com.bc.commonentity.common.JsonResult;
import com.bc.commonentity.enums.HttpReturnRnums;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.util.SystemOutLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @Name OrderController
 * @Description TODO
 * @Author douhaichao
 * @Date 2018/8/28 10:22
 * @Version 1.0
 **/
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    T_OrderService t_orderService;

    @Autowired
    T_Order_kycService t_order_kycService;

    @Autowired
    T_Integral_recordService t_integral_recordService;
    @ApiOperation("查看用户KYC")
    @RequestMapping(value = "getorderkycbyuserid", method = RequestMethod.POST)
    public JsonResult getOrderKycByUserId(@RequestBody OrderKycReq req) {
        SellerUserInfo loginInfo = UserContext.getCurrentContext().getLoginInfo();
        if (loginInfo != null) {
            req.setSeller_user_id(loginInfo.getUser_id());
        }
        return t_order_kycService.getOrderKycBySeller_UserId(req);
    }

    @ApiOperation("获取订单列表")
    @RequestMapping(value = "getorderlist", method = RequestMethod.POST)
    public JsonResult getOrderList(@RequestBody OrderExportReq req) {
        SellerUserInfo loginInfo = UserContext.getCurrentContext().getLoginInfo();
        if (loginInfo == null) {
            return new JsonResult(HttpReturnRnums.NOT_LOGIN);
        }
        req.setSeller_user_id(loginInfo.getUser_id());
        return t_orderService.getOrderList(req);
    }

    @ApiOperation("导出订单列表")
    @RequestMapping(value = "getexportorderlist", method = RequestMethod.GET)
    public void getExportOrderList(HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        SellerUserInfo loginInfo = UserContext.getCurrentContext().getLoginInfo();
        if (loginInfo == null) {
            response.getWriter().write("<script>alert('no login');</script>");
            return;
        }
        OrderExportReq req = new OrderExportReq();
        String email = request.getParameter("email");
        email = StringUtils.isEmpty(email) ? "" : URLDecoder.decode(email, "UTF-8");
        String start_time = request.getParameter("start_time");
        start_time = StringUtils.isEmpty(start_time) ? "" : URLDecoder.decode(start_time, "UTF-8");
        String end_time = request.getParameter("end_time");
        end_time = StringUtils.isEmpty(end_time) ? "" : URLDecoder.decode(end_time, "UTF-8");
        req.setSeller_user_id(loginInfo.getUser_id());
        req.setEmail(email);
        req.setStart_time(start_time);
        req.setEnd_time(end_time);
        t_orderService.getExportOrderList(req, request, response);
    }

    @ApiOperation("测试方法")
    @RequestMapping(value = "test", method = RequestMethod.GET)
    public void test() {
//        String s = WebConfig.getGethUrl();
//        int bug = WebConfig.getIsDebug();
//        System.out.print(bug);
//        System.out.print(s);


    }

}
